package com.cloud.job.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.api.domain.alarm.VehicleAlarm;
import com.cloud.api.domain.report.VehicleAlarmReport;
import com.cloud.api.domain.screen.VehicleBreakdown;
import com.cloud.api.utils.ExcelUtils;
import com.cloud.job.mapper.VehicleAlarmMapper;
import com.cloud.job.service.EmailService;
import com.cloud.job.service.IVehicleAlarmService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.List;
import java.util.Locale;

/**
 * @author 杨斌
 * 2024-12-2024-12-26
 */
@Service("vehicleAlarmService")
public class VehicleAlarmServiceImpl extends ServiceImpl<VehicleAlarmMapper, VehicleAlarm> implements IVehicleAlarmService {
    @Resource
    private VehicleAlarmMapper vehicleAlarmMapper;

    @Resource
    private MessageSource messageSource;

    @Autowired
    private EmailService emailService;
    @Override
    public List<VehicleBreakdown> selectBreakdowns() {
        return vehicleAlarmMapper.selectBreakdowns();
    }

    @Override
    public void exportMailAlarmReportList() {
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.vehicle.offline";
        // 获取上个月的第一天和当前月份的第一天
        // 获取上个月的第一天和当前月份的第一天
        LocalDateTime startTime = YearMonth.now().minusMonths(1).atDay(1).atStartOfDay();
        LocalDateTime endTime = YearMonth.now().atDay(1).atStartOfDay();


        List<VehicleAlarmReport> vehicleeAlarmList = vehicleAlarmMapper.exportMailAlarmReportList(startTime, endTime);
        fileName = messageSource.getMessage(fileName, null, fileName, locale);
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            // 导出Excel文件到ByteArrayOutputStream
            ExcelUtils.export(outputStream, fileName, vehicleeAlarmList, VehicleAlarmReport.class);
            // 获取字节数组副本，用于后续邮件发送
            byte[] fileBytes = outputStream.toByteArray();
            // 异步发送邮件
            emailService.sendEmailAsync("车辆报警数据导出", "请查看附件中的车辆报警数据。", fileName + ".xlsx", fileBytes);
        } catch (IOException e) {
            log.error("导出Excel文件失败", e);
        }
    }

    @Override
    public List<VehicleAlarmReport> getAlarmReportData() {
        // 获取上个月的第一天和当前月份的第一天
        // 获取上个月的第一天和当前月份的第一天
        LocalDateTime startTime = YearMonth.now().minusMonths(1).atDay(1).atStartOfDay();
        LocalDateTime endTime = YearMonth.now().atDay(1).atStartOfDay();
        List<VehicleAlarmReport> vehicleAlarmReports = vehicleAlarmMapper.exportMailAlarmReportList(startTime, endTime);
        vehicleAlarmReports.forEach(vehicleAlarmReport -> {
            if(vehicleAlarmReport.getAlarmType() != null){
                String alarmType = vehicleAlarmReport.getAlarmType().getDesc();
                vehicleAlarmReport.setVehicleAlarmType(alarmType);
            }
        });
        return vehicleAlarmReports;
    }
}
